package com.xlh.service.courseTest.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xlh.service.courseTest.RepositoryImportService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author zhr 2019/6/24
 */
@Service
public class RepositoryImportServiceImpl implements RepositoryImportService {

    // Map<courseId, List<id>>
    private static Map<Long, List<Long>> cacheMap = Maps.newConcurrentMap();

    @Override
    public List<Long> getCache(Long courseId) {
        return cacheMap.get(courseId);
    }

    @Override
    public void putCache(Long courseId, List<Long> ids) {
        List<Long> valueList = cacheMap.get(courseId);
        if (CollectionUtils.isEmpty(valueList)) {
            valueList = Lists.newArrayList();
        }

        if (CollectionUtils.isNotEmpty(ids)) {
            valueList.addAll(ids);
        }

        cacheMap.put(courseId, valueList);
    }

    @Override
    public void removeAllCache(Long courseId) {
        cacheMap.remove(courseId);
    }

    @Override
    public void removeCache(Long courseId, List<Long> ids) {
        List<Long> valueList = cacheMap.get(courseId);
        if (CollectionUtils.isNotEmpty(valueList)) {
            valueList.removeAll(ids);
        }

        cacheMap.put(courseId, valueList);
    }
}
